import UIKit

extension NSString {
    
    var isZipOrPostalCode: Bool {
        var result = false
        let postalRegExString = "(^\\d{5}(-\\d{4})?$)|(^[ABCEGHJKLMNPRSTVXY]{1}\\d{1}[A-Z]{1} *\\d{1}[A-Z]{1}\\d{1}$)"
        var postalRegex: NSRegularExpression?
        do {
            postalRegex = try NSRegularExpression(pattern: postalRegExString, options: .CaseInsensitive)
        } catch { }
        
        result = postalRegex?.numberOfMatchesInString(self as String, options: .ReportProgress, range: NSMakeRange(0, self.length)) > 0
        
        return result
    }
    
    // 验证 Email
    var isValidEmail: Bool {
        let emailRegex = ".+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2}[A-Za-z]*"
        let emailTest = NSPredicate(format: "SELF MATCHES %@", emailRegex)
        
        return emailTest.evaluateWithObject(self)
    }
    
    // 正则判断手机号码地址格式
    var isValidMobileNumber: Bool {
        /**
         * 手机号码
         * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
         * 联通：130,131,132,152,155,156,185,186
         * 电信：133,1349,153,180,189
         */
        let MOBILE = "^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
        /**
         10         * 中国移动：China Mobile
         11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
         12         */
        let CM = "^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
        /**
         15         * 中国联通：China Unicom
         16         * 130,131,132,152,155,156,185,186
         17         */
        let CU = "^1(3[0-2]|5[256]|8[56])\\d{8}$";
        /**
         20         * 中国电信：China Telecom
         21         * 133,1349,153,180,189
         22         */
        let CT = "^1((33|53|8[09])[0-9]|349)\\d{7}$";
        /**
         25         * 大陆地区固话及小灵通
         26         * 区号：010,020,021,022,023,024,025,027,028,029
         27         * 号码：七位或八位
         28         */
        // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
        
        let regextestmobile = NSPredicate(format: "SELF MATCHES %@", MOBILE)
        let regextestcm = NSPredicate(format: "SELF MATCHES %@", CM)
        let regextestcu = NSPredicate(format: "SELF MATCHES %@", CU)
        let regextestct = NSPredicate(format: "SELF MATCHES %@", CT)
        
        if regextestcm.evaluateWithObject(self) {
            print("China Mobile")
        } else if regextestcu.evaluateWithObject(self) {
            print("China Unicom")
        } else if regextestct.evaluateWithObject(self) {
            print("China Telecom")
        } else { print("Unknow") }
        
        return regextestmobile.evaluateWithObject(self)
            || regextestcm.evaluateWithObject(self)
            || regextestcu.evaluateWithObject(self)
            || regextestct.evaluateWithObject(self)
    }
    
    // 验证字母开头
    var isValidLetterPrefix: Bool {
        let regex = "[A-Za-z]+"
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)

        return predicate.evaluateWithObject(self)
    }
    
    // 验证只能是数字
    var isValidNumber: Bool {
        let regex = "^[0-9]+$";
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        
        return predicate.evaluateWithObject(self)
    }
    
    // 验证身份证
    var isIDCard: Bool {
        let regex = "\\d{14}[[0-9],0-9xX]";
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        
        return predicate.evaluateWithObject(self)
    }
    
    // 验证只能是字母
    var isValidOnlyLetter: Bool {
        let regex = "^[A-Za-z0-9]+$";
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        
        return predicate.evaluateWithObject(self)
    }
    
    // 验证整数和小数
    var isValidNumberOrDecimals: Bool {
        let regex = "^[0-9]+([.]{0,1}[0-9]+){0,1}$";
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        
        return predicate.evaluateWithObject(self)
    }
    
    // 验证只能输入汉字
    var isValidOnlyChinese: Bool {
        let regex = "^[\\u4e00-\\u9fa5]{0,}$"
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        
        return predicate.evaluateWithObject(self)
    }
    
    // 验证URL
    var isValidURL: Bool {
        let regex = "^http://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$"
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        
        return predicate.evaluateWithObject(self)
    }
    
    // 验证月份
    var isValidMonth: Bool {
        let regex = "^(0?[1-9]|1[0-2])$"
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        
        return predicate.evaluateWithObject(self)
    }
    
    // 验证日期
    var isValidDay: Bool {
        let regex = "^((0?[1-9])|((1|2)[0-9])|30|31)$"
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        
        return predicate.evaluateWithObject(self)
    }
}